//------------------------------------------------------------------------------
// Include Files
//------------------------------------------------------------------------------
#include <windows.h>
#include <nkintr.h>
#include <tchddsi.h>
#include <tchddi.h>
#include <bsp.h>

#include "touch.h"
#include "args.h"
#include "ioctl_cfg.h"
//------------------------------------------------------------------------------


extern "C" const int MIN_CAL_COUNT = 25;



//DWORD gIntrTouch        = SYSINTR_TOUCH;
//DWORD gIntrTouchChanged = SYSINTR_TOUCH_CHANGED;

#define BSP_ARGS_WM9713_TOUCH	0
#define BSP_ARGS_TSI_TOUCH		1
//shacharh
#define BSP_MICCO_B0_TSI_TOUCH	2

//static DWORD gs_defaultTouchDriver = 0;
static DWORD gs_defaultTouchDriver = BSP_MICCO_B0_TSI_TOUCH;





//extern BOOL WM9713_DdsiTouchPanelEnable();


//------------------------------------------------------------------------------
//shacharh MICCO B0
//------------------------------------------------------------------------------
extern BOOL TSI_MICCO_B0_DdsiTouchPanelEnable(void);
extern void TSI_MICCO_B0_DdsiTouchPanelDisable(void);
extern void	TSI_MICCO_B0_DdsiTouchPanelPowerHandler(BOOL PowerOff);
extern BOOL TSI_MICCO_B0_DdsiTouchPanelGetDeviceCaps( INT iIndex,LPVOID lpOutput);
extern BOOL TSI_MICCO_B0_DdsiTouchPanelSetMode(INT iIndex,LPVOID lpInput);
extern void TSI_MICCO_B0_DdsiTouchPanelGetPoint(TOUCH_PANEL_SAMPLE_FLAGS *pTipStateFlags, INT *pUncalX, INT *pUncalY);
extern LONG TSI_MICCO_B0_DdsiTouchPanelAttach();
extern LONG TSI_MICCO_B0_DdsiTouchPanelDetach();


//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//shacharh MICCO B0
//------------------------------------------------------------------------------
BOOL DdsiTouchPanelEnable(void)
{

	switch (gs_defaultTouchDriver)
	{
	//	case BSP_ARGS_WM9713_TOUCH:
	//		return WM9713_DdsiTouchPanelEnable();
	//	case BSP_ARGS_TSI_TOUCH:
	//		return TSI_DdsiTouchPanelEnable();

		//shacharh
		case BSP_MICCO_B0_TSI_TOUCH:
			return TSI_MICCO_B0_DdsiTouchPanelEnable();
		//shacharh

		default:
		    	RETAILMSG(DEBUG_TOUCH,(_T("[TOUCH] - should not happen in DdsiTouchPanelEnable\r\n")));
			return FALSE;
	}
}

//------------------------------------------------------------------------------
//shacharh MICCO B0
//------------------------------------------------------------------------------

void DdsiTouchPanelDisable(void)
{

	switch (gs_defaultTouchDriver)
	{
		//case BSP_ARGS_WM9713_TOUCH:
		//	return WM9713_DdsiTouchPanelDisable();
		//case BSP_ARGS_TSI_TOUCH:
		//	return TSI_DdsiTouchPanelDisable();
		//shacharh
		case BSP_MICCO_B0_TSI_TOUCH:
			return TSI_MICCO_B0_DdsiTouchPanelDisable();
		//shacharh

		default:
		    	RETAILMSG(DEBUG_TOUCH,(_T("[TOUCH] - should not happen in DdsiTouchPanelDisable\r\n")));
			return;
	}
}

//------------------------------------------------------------------------------
//shacharh MICCO B0
//------------------------------------------------------------------------------

void DdsiTouchPanelPowerHandler(BOOL PowerOff)
{
 	switch (gs_defaultTouchDriver)
	{
		//case BSP_ARGS_WM9713_TOUCH:
		//	return WM9713_DdsiTouchPanelPowerHandler(PowerOff);
		//case BSP_ARGS_TSI_TOUCH:
		//	return TSI_DdsiTouchPanelPowerHandler(PowerOff);

		//shacharh
		case BSP_MICCO_B0_TSI_TOUCH:
			return TSI_MICCO_B0_DdsiTouchPanelPowerHandler(PowerOff);
		//shacharh


		default:
		    	RETAILMSG(DEBUG_TOUCH,(_T("[TOUCH] - should not happen in DdsiTouchPanelPowerHandler\r\n")));
			return;
	}
}

//------------------------------------------------------------------------------
//shacharh MICCO B0 - Called from tchbasic -> DLL_PROCESS_ATTACH
//------------------------------------------------------------------------------
LONG DdsiTouchPanelAttach()
{
	switch (gs_defaultTouchDriver)
	{
	case BSP_MICCO_B0_TSI_TOUCH:
		return TSI_MICCO_B0_DdsiTouchPanelAttach();
		break;
	default:
		RETAILMSG(DEBUG_TOUCH,(_T("[TOUCH] - should not happen in DdsiTouchPanelAttach\r\n")));
		break;
	}
	return FALSE;

}
//------------------------------------------------------------------------------
//shacharh MICCO B0 - Called from tchbasic -> DLL_PROCESS_DETACH
//------------------------------------------------------------------------------
LONG DdsiTouchPanelDetach()
{
	switch (gs_defaultTouchDriver)
	{
	case BSP_MICCO_B0_TSI_TOUCH:
		return TSI_MICCO_B0_DdsiTouchPanelDetach();
		break;
	default:
		RETAILMSG(DEBUG_TOUCH,(_T("[TOUCH] - should not happen in DdsiTouchPanelDetach\r\n")));
		break;
	}
	return 0;
	
    
}
//------------------------------------------------------------------------------
//shacharh MICCO B0
//------------------------------------------------------------------------------
extern "C" BOOL DdsiTouchPanelGetDeviceCaps(INT iIndex,LPVOID  lpOutput)
{
	switch (gs_defaultTouchDriver)
	{
		//case BSP_ARGS_WM9713_TOUCH:
		//	return WM9713_DdsiTouchPanelGetDeviceCaps( iIndex,lpOutput);
		//case BSP_ARGS_TSI_TOUCH:
		//	return TSI_DdsiTouchPanelGetDeviceCaps( iIndex,lpOutput);

		//shacharh
		case BSP_MICCO_B0_TSI_TOUCH:
			return TSI_MICCO_B0_DdsiTouchPanelGetDeviceCaps( iIndex,lpOutput);
		//shacharh

		default:
		    	RETAILMSG(DEBUG_TOUCH,(_T("[TOUCH] - should not happen in DdsiTouchPanelGetDeviceCaps\r\n")));
			return (FALSE);
	}
}

//------------------------------------------------------------------------------
//shacharh MICCO B0
//------------------------------------------------------------------------------

BOOL DdsiTouchPanelSetMode(INT iIndex,LPVOID  lpInput)
{
	switch (gs_defaultTouchDriver)
	{
		//case BSP_ARGS_WM9713_TOUCH:
		//	return WM9713_DdsiTouchPanelSetMode( iIndex,lpInput);
		//case BSP_ARGS_TSI_TOUCH:
		//	return TSI_DdsiTouchPanelSetMode( iIndex,lpInput);
		//shacharh
		case BSP_MICCO_B0_TSI_TOUCH:
			return TSI_MICCO_B0_DdsiTouchPanelSetMode( iIndex,lpInput);
		//shacharh
		default:
		    	RETAILMSG(DEBUG_TOUCH,(_T("[TOUCH] - should not happen in DdsiTouchPanelSetMode\r\n")));
				return FALSE;

	}
}

//------------------------------------------------------------------------------
//shacharh MICCO B0
//------------------------------------------------------------------------------

void DdsiTouchPanelGetPoint(TOUCH_PANEL_SAMPLE_FLAGS *pTipStateFlags, INT *pUncalX, INT *pUncalY)
{
	//RETAILMSG(DEBUG_TOUCH,(_T("DdsiTouchPanelGetPoint\r\n")));
	
	switch (gs_defaultTouchDriver)
	{
		//case BSP_ARGS_WM9713_TOUCH:
		//	return WM9713_DdsiTouchPanelGetPoint(pTipStateFlags,pUncalX,pUncalY);
		//case BSP_ARGS_TSI_TOUCH:
		//	return TSI_DdsiTouchPanelGetPoint(pTipStateFlags,pUncalX,pUncalY);
		case BSP_MICCO_B0_TSI_TOUCH:
			return TSI_MICCO_B0_DdsiTouchPanelGetPoint(pTipStateFlags,pUncalX,pUncalY);


		default:
		    	RETAILMSG(DEBUG_TOUCH,(_T("[TOUCH] - should not happen in DdsiTouchPanelGetPoint\r\n")));
			return ;
	}
}

//------------------------------------------------------------------------------
//shacharh MICCO B0
//------------------------------------------------------------------------------


extern "C" BOOL TouchDriverCalibrationPointGet(TPDC_CALIBRATION_POINT *pTCP)
{
    INT32 cDisplayWidth = pTCP->cDisplayWidth;
    INT32 cDisplayHeight = pTCP->cDisplayHeight;

    int CalibrationRadiusX = cDisplayWidth/10;
    int CalibrationRadiusY = cDisplayHeight/10;

	//RETAILMSG(1,(_T("+ TouchDriverCalibrationPointGet\r\n")));

    switch (pTCP->PointNumber)
    {
    case  0:  // Middle
        pTCP->CalibrationX = cDisplayWidth/2;
        pTCP->CalibrationY = cDisplayHeight/2;
        break;

    case  1:  // Upper Left
        pTCP->CalibrationX = CalibrationRadiusX*2;
        pTCP->CalibrationY = CalibrationRadiusY*2;
        break;

    case  2:  // Lower Left
        pTCP->CalibrationX = CalibrationRadiusX*2;
        pTCP->CalibrationY = cDisplayHeight - CalibrationRadiusY*2;
        break;

    case  3:  // Lower Right
        pTCP->CalibrationX = cDisplayWidth - CalibrationRadiusX*2;
        pTCP->CalibrationY = cDisplayHeight - CalibrationRadiusY*2;
        break;

    case  4:  // Upper Right
        pTCP->CalibrationX = cDisplayWidth - CalibrationRadiusX*2;
        pTCP->CalibrationY = CalibrationRadiusY*2;
        break;

    default:
        pTCP->CalibrationX = cDisplayWidth/2;
        pTCP->CalibrationY = cDisplayHeight/2;
        SetLastError(ERROR_INVALID_PARAMETER);
        return (FALSE);
    }


	//RETAILMSG(1,(_T("- TouchDriverCalibrationPointGet\r\n")));
    return (TRUE);
}

//------------------------------------------------------------------------------
//shacharh MICCO B0
//------------------------------------------------------------------------------

extern"C" BOOL TouchPanelDllEntry(
    HANDLE hDllHandle,
    DWORD  dwReason,
    LPVOID lpReserved
    );

extern "C" BOOL DllEntry(
    HANDLE hDllHandle,
    DWORD  dwReason,
    LPVOID lpReserved
    )
{
	BSP_ARGS args;
	DWORD size;
	if(KernelIoControl(IOCTL_GET_BSP_ARGS,NULL,0,&args,sizeof(args),&size))
	{
		gs_defaultTouchDriver = args.DefaultTouchDriver;
#ifndef BSP_MONAHANS_P
		gs_defaultTouchDriver = BSP_ARGS_WM9713_TOUCH;
#else
		gs_defaultTouchDriver = BSP_ARGS_TSI_TOUCH;
#endif
//shacharh	force for Micco B0
		gs_defaultTouchDriver = BSP_MICCO_B0_TSI_TOUCH;



		// Yu Tang
		RETAILMSG(1, (TEXT("[TOUCH] - TSI: %s\r\n"), gs_defaultTouchDriver?L"Enabled":L"Disabled"));

	}
       return TouchPanelDllEntry(hDllHandle,dwReason,lpReserved);

}

//------------------------------------------------------------------------------

//shacharh end
